<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2021/3/4 0004 15:20
 * Describe: 微信企业模块 - 微信企业类
 */

namespace app\manage\controller\corp;

use app\common\model\corp\Corp as CorpModel;

class Corp extends Base
{
    /**
     * 添加企业
     */
    public function addCorp()
    {
        $postData = request()->only('corpid,corp_full_name,subject_type,corp_qrcode,type,way');
        $rule = [
            'corpid|企业ID' => 'require',
            'corp_full_name|企业全称' => 'require',
            'subject_type|企业类型' => 'require|in:1,2,3,4',
            'corp_qrcode|企业二维码' => 'require',
            'type|账户类型' => 'in:0,1,2',
            'way|获取方式' => 'require'
        ];
        $this->validate($postData, $rule);

        if(count($postData['way']) > 2){
            check_exception('获取方式仅限添加两个');
        }
        foreach ($postData['way'] as $k=>$v){
            $tempRule = [
                'type|类型' => 'require|in:1,2',
                'content|内容' => 'require',
            ];
            if($v['type'] == 2){
                $tempRule['keyword|关键字'] = 'require';
            }
            $this->validate($v, $tempRule);
        }
        $postData['admin_id'] = $this->tokenAndAdminInfo['id'];
        (new CorpModel)->addCorp($postData);

        return return_formart(true);
    }

    /**
     * 编辑
     */
    public function editCorp()
    {
        $postData = request()->only('id,corpid,corp_full_name,subject_type,corp_qrcode,type,way');
        $rule = [
            'id|ID' => 'require',
            'corpid|企业ID' => 'require',
            'corp_full_name|企业全称' => 'require',
            'subject_type|企业类型' => 'require|in:1,2,3,4',
            'corp_qrcode|企业二维码' => 'require',
            'type|账户类型' => 'in:0,1,2',
            'way|获取方式' => 'require'
        ];
        $this->validate($postData, $rule);

        if(count($postData['way']) > 2){
            check_exception('获取方式仅限添加两个');
        }
        foreach ($postData['way'] as $k=>$v){
            $tempRule = [
                'type|类型' => 'require|in:1,2',
                'content|内容' => 'require',
            ];
            if($v['type'] == 2){
                $tempRule['keyword|关键字'] = 'require';
            }
            $this->validate($v, $tempRule);
        }

        $postData['admin_id'] = $this->tokenAndAdminInfo['id'];
        (new CorpModel)->editCorp($postData);

        return return_formart(true);
    }

    /**
     * 获取企业信息
     */
    public function getCorpInfo()
    {
        $postData = request()->only('auth_code,id');
        if(isset($postData['auth_code']) && !empty($postData['auth_code'])){
            $where['auth_code'] = $postData['auth_code'];
        }
        if(isset($postData['id']) && !empty($postData['id'])){
            $where['id'] = $postData['id'];
        }
        if(empty($where)){
            check_exception('缺少参数auth_code或id');
        }

        $corpInfo = (new CorpModel())->where($where)->field('id,corpid,corp_full_name,subject_type,corp_qrcode,auth_info,type')->find();
        if($corpInfo){
            $corpInfo['corp_qrcode_url'] = get_img_url($corpInfo['corp_qrcode']);
        }

        $corpInfo['way'] = db('wechat_corp_way')->where('corp_id', $corpInfo['id'])->field('type,keyword,content')->select();
        return return_formart($corpInfo);
    }

    /**
     * 获取企业列表
     */
    public function getCorpList()
    {
        $postData = request()->only('start_time,end_time,name,page,limit');

        $where = [];
        if(isset($postData['start_time']) && isset($postData['end_time']) && !empty($postData['start_time']) && !empty($postData['end_time'])){
            $where['c.create_time'] = ['between time', [$postData['start_time'], $postData['end_time'] . '23:59:59']];
        }
        if(isset($postData['name']) && !empty($postData['name'])){
            $where['c.corp_full_name'] = ['like', '%' . $postData['name'] . '%'];
        }

        $count = db('wechat_corp')->alias('c')->where($where)->count('id');
        $temp = db('wechat_corp')->alias('c')
            ->join('w_account a', 'c.admin_id=a.id', 'LEFT')
            ->where($where)
            ->field('c.id,c.corpid,c.create_time,c.corp_qrcode,c.corp_full_name,c.type,c.auth_status,a.username')
            ->order('create_time', 'desc')
            ->page($postData['page']?? 1, $postData['limit']?? 10)
            ->select();
        if($temp){
            foreach ($temp as &$v){
                $v['create_time'] = date('Y-m-d', $v['create_time']);
                $v['task_num'] = 0;
                $v['corp_qrcode'] = get_img_url($v['corp_qrcode']);
            }
        }

        return json(['code' => 200, 'msg' => 'ok', 'count' => $count, 'content' => $temp]);
    }

}